﻿$(document).ready(function () {
    /*
     * recipe's step view
     */
    $("[data-type=stepview]").click(function () {
        layer.load(2);
        var recipe_id = $(this).attr("data-recipeid");
        var api_url = window.singleConfig.rootUrl + "api/common/RecipeService/getStepsByRecipeID?recipe_id=" + recipe_id;
        var stepImgPath = window.singleConfig.rootUrl + "public/data/uploadStepFile/";
        var noimage = window.singleConfig.rootUrl + "public/images/basic/step-no-image-660-420.svg";

        $.get(api_url, {}, function (data) {
            if (data.code == "success") {
                layer.closeAll();            
                
                var lightbox_items = [];
                var stepscount = data.steps.length;

                $.each(data.steps, function (index, item) {
                    var title = "Step " + (index + 1) + "/" + stepscount + ": " + item.meta_key;
                    if (item.meta_value != "") {
                        lightbox_items.push({ 'source': stepImgPath + item.meta_value, 'type': 'image', 'title': title });
                    }
                    else {
                        lightbox_items.push({ 'source': noimage, 'type': 'image', 'title': title });
                    }
                });

                if (lightbox_items.length == 0) {
                    layer.msg("No raw or step !");
                }
                else {
                    var lightbox = UIkit.lightbox.create(lightbox_items);
                    lightbox.show();
                }                

            }
            else {
                layer.closeAll();
                layer.msg(data.message);
            }
        }, "json");
    });

    /*
     * collect recipe
     */
    $("[data-type=collectRecipe]").click(function () {
        layer.load(2);
        var status = $(this).attr("data-status");
        var xkdemdkf = $(this).attr("data-xkdemdkf");
        var api_url = window.singleConfig.rootUrl + "api/users/RecipeService/" + status + "?xkdemdkf=" + xkdemdkf;
        var $obj = $(this);

        $.get(api_url, {}, function (data) {
            if (data.code == "success") {
                layer.closeAll();
                if (status == "collect") {
                    $obj.attr("data-status", "uncollect");
                    $obj.attr("src", window.singleConfig.rootUrl + "/public/images/icon/collect_64_64.png");
                    layer.msg("Collect this recipe successful !");
                    $obj.attr("title", 'click to uncollect this recipe');
                }
                else {
                    $obj.attr("data-status", "collect");
                    $obj.attr("src", window.singleConfig.rootUrl + "/public/images/icon/collect_gray_64_64.png");
                    layer.msg("Uncollect this recipe successful !");
                    $obj.attr("title", 'click to collect this recipe');
                }                
            }
            else {
                layer.closeAll();
                layer.msg(data.message);
            }
        }, "json");
    });

    /*
     * collect recipe
     */
    $("[data-type=removeFromCollection]").click(function () {
        var $obj = $(this);

        layer.confirm('Are you sure to remove this recipe ?', { icon: 3, title: 'Message', btn: ['Confirm', 'Cancel'] }, function (index) {
            layer.load(2);
            var xkdemdkf = $obj.attr("data-xkdemdkf");
            var api_url = window.singleConfig.rootUrl + "api/users/RecipeService/uncollect?xkdemdkf=" + xkdemdkf;

            $.get(api_url, {}, function (data) {
                if (data.code == "success") {
                    $obj.parents(".uk-width-1-2:first").remove();
                    layer.closeAll();
                }
                else {
                    layer.closeAll();
                    layer.msg(data.message);
                }
            }, "json");
        });
    });

    /*
     * cooker page, follow button
     */
    $("#btnFollow").click(function () {
        var $btnFollow = $("#btnFollow");
        var method = $btnFollow.hasClass("uk-button-danger") == true ? "follow" : "unfollow";

        layer.load(2);
        var api_url = window.singleConfig.rootUrl + "api/users/CookerService/" + method + "?cookerid=" + window.singleConfig.cookerid;
        $.get(api_url, {}, function (data) {
            layer.closeAll();
            if (data.code == "error") {
                layer.msg(data.message);
            }
            else {
                layer.msg(method == "follow" ? "Follow successful !" : "UnFollow successful !");
                if (method == "follow") {
                    $btnFollow.removeClass("uk-button-danger").addClass("uk-button-default");
                   
                }
                else {
                    $btnFollow.removeClass("uk-button-default").addClass("uk-button-danger");
                  
                }
            }
        }, "json");
    });

    /*
     * delete recipe
     */
    $("[data-type=deleteRecipe]").click(function () {
        var id = $(this).attr("data-recipeid");
        layer.confirm('Are you sure to delete this recipe ?', { icon: 3, title: 'Message', btn: ['Confirm', 'Cancel'] }, function (index) {
            layer.load(2);
            var api_url = window.singleConfig.rootUrl + "api/users/RecipeService/delete?id=" + id;
            $.get(api_url, {}, function (data) {
                layer.closeAll();
                layer.msg("Delete successful !");
                $("a[data-recipeid=" + id + "]").parents(".trend:first").parent().remove();
            }, 'json');
        });
    });

    if ($("#liNotifications").length > 0) {
        var currentTime = new Date().getTime();
        if (localStorage.last_get_message_count == undefined || (currentTime - parseInt(localStorage.last_get_message_count)) > 60000) {
            $.get(window.singleConfig.rootUrl + "api/users/MessageService/getUnReadMessageCount", {}, function (result) {
                var html = result.code == 'success' && parseInt(result.count) > 0 ? "<span class='notifications'>" + result.count + "</span>" : "";
                $("#liNotifications").html(html);
                localStorage.setItem('last_get_message_count', currentTime);
                localStorage.setItem('last_message_count', html == "" ? 0 : result.count);
            }, 'json');
        }
        else if (localStorage.last_message_count != undefined && parseInt(localStorage.last_message_count) > 0) {
            $("#liNotifications").html("<span class='notifications'>" + localStorage.last_message_count + "</span>");
        }
    }
});